<?php
include_once("../root.inc.php");
include_once("../mysql/database_info.php");
include_once("../mysql/common.php");
include_once("../common.php");
// Read the notification from PayPal and create the acknowledgement response
$req = "cmd=_notify-validate";               // add 'cmd' to beginning of the acknowledgement you send back to PayPal
foreach ($_POST as $key => $value) {         // Loop through the notification NV pairs
	$value = urlencode(stripslashes($value));  // Encode the values
	$req .= "&$key=$value";                    // Add the NV pairs to the acknowledgement
}
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$vars = explode("|row|", $_POST['custom']);
$payer_email = $_POST['payer_email'];
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: ipnpb.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen('ssl://ipnpb.paypal.com', 443, $errno, $errstr, 30);
fputs($fp, $header . $req);
$x = stream_get_contents($fp);
$y = stristr($x, "VERIFIED");
if ($y != FALSE) {  // Response is VERIFIED
  if (($payment_status == "Completed") && ($vars != false) && (count($vars) > 1)){
		connect_select_test("events");
		$id = array_shift($vars);
		$paid = array_pop($vars);
		$scholarship = array_pop($vars);
		$paymentType = array_pop($vars);
		$form = mysql_fetch_array(mysql_query("SELECT * FROM `registrationForms` WHERE `event`='$id'"));
		for ($x=1; $x<=count($form);$x++){
			if (!empty($form[$x."Type"])){
				$types[] = $form[$x."Type"];
				$names[] = $form[$x."Name"];
				$placeholders[] = $form[$x."Placeholder"];
			}
		}
		if (!table_exists("Registration_$id", "southhi9_shcc")){
			$newTable = "CREATE TABLE `Registration_$id` (
				  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,";
				  for ($x=0;$x<count($names);$x++){
					  $newTable.="`".$names[$x]."` text,";
				  }
				  $newTable.= "`paid` tinyint(1),";
				  $newTable.= "`scholarship` tinyint(1),";
				  $newTable.= "`paymentType` text,";
				  $newTable.= "`grandTotal` float,";
				  $newTable.="PRIMARY KEY (`id`)) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
			$test = mysql_query($newTable);
			if ($test){
				mail("codys@southhillcalvary.org", "Success at ".date("Y-m-d h:i:s", time()), "Created Registration_$id");
			} else {
				mail("codys@southhillcalvary.org", "Failure", "Failure to make Registration_$id, ".$newTable);
			}
		}
		//real query
		$insert = "INSERT INTO `Registration_$id` VALUES(";
		$insert.="'', ";
		for ($x=0;$x<count($names);$x++){
			$insert.="'".$vars[$x]."', ";
		}
		$insert.="'".$paid."', ";
		$insert.="'".$scholarship."', ";
		$insert.="'".$paymentType."', ";
		$insert.="'".$payment_amount."')";
		if (mysql_query($insert)){
			mail("codys@southhillcalvary.org", "New Registration", "Registered ".implode(", ", $vars));
		} else {
			mail("codys@southhillcalvary.org", "New Registration Failed", "Couldn't register ".$insert);
		}

	}
}
else { 
	mail("codys@southhillcalvary.org", "Failure", "INVALID IPN in file:\n\n".$x);
}
fclose ($fp);  //close file pointer
?>